<template>
    <div>
        <!-- bobo视频播放系统开发日志 -->
        <input :id="id" type="file" style="display: none;" @change="onFileChange" accept="image/*">
        <div v-if="!value" @click="selectFile" class="upload-placeholder">
            <span>+</span>
            <span>{{ value && value != '' }}</span>
        </div>
        <img @click="selectFile" decoding="async" v-else :src="value" alt="Smiley face" class="my-image">

        <div>
            <a-modal :title="aspectRatio" :visible="visible" :confirm-loading="confirmLoading" @ok="handleOk"
                @cancel="handleCancel">
                <cropper ref="cropper" class="cropper" :src="croppedImage" :stencil-props="{
                        aspectRatio: aspectRatio
                    }" @change="changeCropper" />
            </a-modal>

        </div>
    </div>
</template>
  
<script>
import { Cropper } from 'vue-advanced-cropper'
import 'vue-advanced-cropper/dist/style.css';
import { httpAction, getAction } from '@/api/manage'
export default {
    props: {
        value: String ,// 声明value属性
        aspectRatio:Number
    },
    components: { Cropper },
    data() {
        return {
            id:'file-upload'+new Date().getTime(),
            imageFile: null,
            croppedImage: null,
            ModalText: 'Content of the modal',
            visible: false,
            confirmLoading: false,
            imgUrl: ''
        }
    },

    model: {
        prop: 'value',
        event: 'input'
    },
    methods: {
        selectFile() {
            document.getElementById(this.id).click();
        },
        changeCropper({ coordinates, canvas }) {
            console.log(coordinates, canvas)
        },
        onFileChange(event) {
            this.imageFile = event.target.files[0]
            const reader = new FileReader()
            reader.readAsDataURL(this.imageFile)
            reader.onload = () => {
                this.croppedImage = reader.result;
                this.visible = true;
            }
        },
        async getCroppedImage() {
            const { canvas } = this.$refs.cropper.getResult();
            if (canvas) {
                console.log(canvas, "canvas");
                const form = new FormData();
                canvas.toBlob(blob => {
                    console.log(blob, "blob");
                    form.append('file', blob, 'image.jpg');
                    console.log(form, "form");
                    // You can use axios, superagent and other libraries instead here
                    // fetch('http://example.com/upload/', {
                    //     method: 'POST',
                    //     body: form,
                    // });
                    // Perhaps you should add the setting appropriate file format here
                    httpAction(window._CONFIG['domianURL'] + '/upload/file', form, 'post').then((res) => {
                        if (res.success) {
                            // this.value = res.result;
                            // this.$emit('change', res.result);
                            this.$emit('input', res.result)
                        } else {

                        }
                    }).finally(() => {

                    })
                }, 'image/jpeg');
            }
            // 在这里上传 croppedBlob 到后端

            this.visible = false;
            this.confirmLoading = false;
        },
        showModal() {
            this.visible = true;
        },
        handleOk(e) {
            this.ModalText = '请稍后，图片裁剪中';
            this.confirmLoading = true;

            setTimeout(() => {
                this.getCroppedImage();
            }, 2000);
        },
        handleCancel(e) {
            console.log('Clicked cancel button');
            this.visible = false;
        },
    }
}
</script>

<style scoped>
.upload-placeholder {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 100px;
    height: 100px;
    color: #ccc;
    font-size: 14px;
    text-align: center;
    background: rgb(149, 149, 149);
    cursor: pointer;
}

.upload-placeholder span:first-child {
    font-size: 32px;
    margin-bottom: 8px;
}

.my-image {
    cursor: pointer;
    max-width: 100px;
    /* 图片最大宽度为父元素宽度 */
    max-height: 100px;
    /* 图片最大高度为父元素高度 */
    width: auto;
    /* 宽度自适应 */
    height: auto;
    /* 高度自适应 */
}
</style>